<?php


namespace app\admin\controller;

use think\Controller;
use think\Db;

class Admin extends Base
{
    public function add()
    {

        if (request()->isAjax()) {
            $data = input('');
            if (!$data['username']) {
                $this->error('请输入账户');
            }
            $ret = Db::name('admin')->where('username', $data['username'])->find();
            if ($ret) {
                $this->error('账号已存在');
            }


            if (!$data['password']) {
                $this->error('请输入密码');
            }
            if (!$data['repassword']) {
                $this->error('请输入密码');
            }
            if (!$data['phone']) {
                $this->error('请输入联系方式');
            }
            if ($data['password'] != $data['repassword']) {
                $this->error('两次密码不一致');
            }
            unset($data['repassword']);
            $data['password'] = cmf_password($data['password']);

            Db::name('admin')->insertGetId($data);
            $this->success('添加成功');
        } else {
            return $this->fetch();
        }


    }


    /**
     * {
     * code: 0,
     * msg: "",
     * count: 1000,
     * data: []
     * }
     */
    public function index()
    {
        if (request()->isAjax()) {
            $status[0] = '禁用';
            $status[1] = '正常';

            $res = Db::name('admin')->select();
            foreach ($res as $index => $re) {
                $res[$index]['status2'] = $status[$re['status']];
            }

            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
        } else {
            return $this->fetch();
        }


    }

    public function info()
    {

        $id = input('id');
        if (!$id) {
            $this->error('id 异常');
        }

        $info = Db::name('admin')
            ->find($id);
        $this->success('ok', '', $info);

    }


    public function del()
    {
        $id = input('id');
        if (!$id) {
            $this->error('id 异常');
        }
        Db::name('admin')->where('id', $id)->update(['status' => -1]);
        $this->success('成功');
    }

    public function edit()
    {

        if (request()->isAjax()) {
            $data = input('');
            if ($data['password']) {
                $data['password'] = cmf_password($data['password']);
            }

            if (!$data['phone']) {
                $this->error('请输入联系方式');
            }

            $res = Db::name('admin')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('admin')->where('status', '>', 0)->find($id);
            $this->assign('item', $res);
            return $this->fetch();
        }


    }

    // {
    // "code": 1
    // ,"msg": ""
    // ,"data": {
    // "username": "贤心"
    // ,"sex": "男"
    // ,"role": 1
    // }
    // }

    public function get_session()
    {
        $data = [];
        $data['username'] = $this->admin_info['username'];
        return json(['code' => 1, 'data' => $data]);

    }


    public function password()
    {

        if (request()->isAjax()) {

            $data = input('');
            $oldpassword = trim($data['oldpassword']);
            $password = trim($data['password']);
            $repassword = trim($data['repassword']);
            if (!$oldpassword || !$password || !$repassword) {
                $this->error('数据异常');
            }

            if ($password != $repassword) {
                $this->error('两次密码不一致');
            }

            $pass = Db::name('admin')->where('id', $this->admin_id)->value('password');
            if ($pass != cmf_password($oldpassword)) {
                $this->error('密码错误');
            }
            Db::name('admin')->where('id', $this->admin_id)->update(['password' => cmf_password($password)]);
            $this->success('修改成功');


        }


    }


}
